#!/bin/bash

# 定义常量
DATE_TAG_FORMAT="%Y%m%d%H%M%S"
IMAGE_NAME="yunyao-jimureport"
CONTAINER_NAME="yunyao-jimureport-container"
JAR_NAME="yunyao-jimureport-1.7.jar"

# 获取当前日期，格式为 YYYYMMDD
DATE_TAG=$(date +"$DATE_TAG_FORMAT")

# 检查是否存在同名的容器，如果存在则停止并删除
EXISTING_CONTAINER=$(docker ps -aq -f name=$CONTAINER_NAME)
if [ -n "$EXISTING_CONTAINER" ]; then
    echo "Stopping and removing existing container: $CONTAINER_NAME"
    docker stop $CONTAINER_NAME || true
    docker rm $CONTAINER_NAME || true
fi

# 构建 Docker 镜像
docker build -t $IMAGE_NAME:$DATE_TAG .

# 运行 Docker 容器
docker run -d -p 8085:8085 -e LANG=C.UTF-8 -e LC_ALL=C.UTF-8 --name $CONTAINER_NAME $IMAGE_NAME:$DATE_TAG

echo "Docker image with tag $DATE_TAG has been built and the container named $CONTAINER_NAME is running."